package com.example.commonlib.commonutil;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.commonlib.base.MyApplication;

/**
 * Created by xjs
 * on 2016/11/5.
 */

public class SPUtils {
    /**
     * 保存在手机里面的文件名
     */
    public static final String USER_FILE = "user_config";
    public static final String TYPE_FILE = "type_file";

    public static final String TOKEN = "token";

    public static final String PHONE = "phone";
    public static final String PASSWORD = "password";

    public static final String NICK = "nick_name";
    public static final String HEAD = "head_url";

    public static final String WXID = "weixin_id";
    public static final String FIRST_LOGIN = "first_login";
    public static final String FIRST_INSTALLATION = "first_installation";


    public static void setToken(String weight) {
        setUserParam(TOKEN, weight);
    }

    public static String getToken() {
        return (String) getUserParams(TOKEN, "");
    }

    public static void setBandedPhone(String phone) {
        setUserParam(PHONE, phone);
    }


    public static String getPhone(){
        return (String) getUserParams(PHONE, "");
    }


    public static void setBandedPassword(String password) {
        setUserParam(PASSWORD, password);
    }


    public static String getPassWord(){
        return (String) getUserParams(PASSWORD, "");
    }


    public static void setNick(String nick) {
        setUserParam(NICK, nick);
    }

    public static String getNick(){
        return (String) getUserParams(NICK, "");
    }


    public static void setHead(String headUrl) {
        setUserParam(HEAD, headUrl.replace("\\/", "/"));
    }

    public static String getHead() {
        return (String) getUserParams(HEAD, "");
    }


    public static void setWxId(String id) {
        setUserParam(WXID, id);
    }

    public static String getWxId() {
        return (String) getUserParams(WXID, "");
    }


    public static void setFirstLogin(boolean firstLogin) {
        setParams(TYPE_FILE, MyApplication.isIntance, FIRST_LOGIN, firstLogin);
    }

    public static boolean isFirstLogin() {
        return (boolean) getParam(TYPE_FILE, MyApplication.isIntance, FIRST_LOGIN, true);
    }

    public static void setFirstInstallation(boolean firstInstallation) {
        setParams(TYPE_FILE, MyApplication.isIntance, FIRST_INSTALLATION, firstInstallation);
    }

    public static boolean isFirstInstallation() {
        return (boolean) getParam(TYPE_FILE, MyApplication.isIntance, FIRST_INSTALLATION, true);
    }

    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     *
     * @param key
     * @param value
     */
    public static void setUserParam(String key, Object value) {
        setParams(USER_FILE, MyApplication.isIntance, key, value);
    }

    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     *
     * @param context
     */
    public static boolean setParams(final String fileName, Context context, String key, Object value) {
        String type = value.getClass().getSimpleName();
        SharedPreferences sp = context.getApplicationContext().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        if ("String".equals(type)) {
            editor.putString(key, (String) value);
        } else if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) value);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) value);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) value);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) value);
        }
        return editor.commit();
    }


    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     *
     * @param context
     * @param key
     * @param defaultObject
     * @return
     */
    public static Object getParam(String fileName, Context context, String key, Object defaultObject) {
        String type = defaultObject.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        if ("String".equals(type)) {
            try {
                return sp.getString(key, (String) defaultObject);
            } catch (ClassCastException e) {
                try {
                    return sp.getInt(key, Integer.parseInt(defaultObject.toString())) + "";
                }catch (Exception ex) {
                    return "0";
                }
            }

        } else if ("Integer".equals(type)) {
            return sp.getInt(key, (Integer) defaultObject);
        } else if ("Boolean".equals(type)) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        } else if ("Float".equals(type)) {
            return sp.getFloat(key, (Float) defaultObject);
        } else if ("Long".equals(type)) {
            return sp.getLong(key, (Long) defaultObject);
        }
        return defaultObject;
    }

    public static Object getUserParams(String key, Object defaultValue) {
        return getParam(USER_FILE, MyApplication.isIntance, key, defaultValue);
    }
}
